package com.youthburin.base.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

public class BaseController {
	private ApplicationContext context;

	public String getMessage(String code, HttpServletRequest request) {
		return getContext(request).getMessage(code, null, code,
				(Locale) request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME));
	}

	public String getMessage(String code, Object[] args, HttpServletRequest request) {
		return getContext(request).getMessage(code, args, code,
				(Locale) request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME));
	}

	public ApplicationContext getContext(HttpServletRequest request) {
		if (context == null)
			context = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());

		return context;
	}
}
